package com.cqgcxy.shopping.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cqgcxy.shopping.common.CommException;
import com.cqgcxy.shopping.common.JdbcUtils;
import com.cqgcxy.shopping.domain.User;


/**
 * 1.该类主要定义一些数据库操作的方法(包含增删改查的方法)。
 * 2.只会被业务类的方法调用。
 * 3.也只有在该类中才允许执行sql语句进行数据库操作。
 * @author 方昆
 */
public class UserDao {
	private Logger logger=LoggerFactory.getLogger(getClass());
	
	private User rowMap(ResultSet rs) throws SQLException{
		User user=new User();
		user.setUsercode(rs.getString("usercode"));
		//user.setUsername(rs.getString("username"));
		user.setPhonenumber(rs.getString("phonenumber"));
		//user.setEmail(rs.getString("email"));
		user.setPassword(rs.getString("password"));
		//user.setUsertype(rs.getInt("usertype"));
		return user;
	}
	
	public  User findByCode(String usercode,String password) {
		Connection conn = JdbcUtils.getConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;
	try {
		String sql="select * from user where usercode=? and password=?";
		ps=conn.prepareStatement(sql);
		ps.setString(1, usercode);
		ps.setString(2, password);
		logger.debug(sql);
		rs=ps.executeQuery();
		
		while (rs.next()) {
			return rowMap(rs);
		}
	}catch (Exception e) {
		logger.error(e.getMessage());
		throw new CommException(e.getMessage());
	}finally {
		JdbcUtils.close(conn, ps, rs);
	}
	return null;
	}
	
	public void insert(User user) {
			Connection conn = JdbcUtils.getConnection();
			PreparedStatement ps=null;
			ResultSet rs=null;
		try {
			String sql="insert into user(usercode,password,phonenumber) values(?,?,?)";
			ps=conn.prepareStatement(sql);
			ps.setString(1, user.getUsercode());
			ps.setString(2, user.getPassword());
			ps.setString(3, user.getPhonenumber());
			
			if(ps.executeUpdate()< 1) {
				throw new CommException("插入数据异常");
			}
			logger.debug("插入数据成功");
		}catch (Exception e) {
			throw new CommException("插入数据异常:"+e.getMessage());
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
		
	}
	public void update(User user) {
		Connection conn=JdbcUtils.getConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			logger.debug("修改数据");
			String sql="update user set user_name=?,password=? where usercode=?";
			ps=conn.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPassword());
			ps.setString(3, user.getUsercode());
			
			logger.debug(sql);
			
			if(ps.executeUpdate()!=1) {
				throw new CommException("修改数据失败");
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new CommException(e.getMessage());
		} finally {
			JdbcUtils.close(conn, ps, rs);
		}

	}
	
	
	
	public void xiugaixinxi(String username,String userzhenshiname,String user_sex,String user_nian,String user_yue,String user_day,String email,String phonenumber,String usercode) {
	
		Connection conn=JdbcUtils.getConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;
		String user_date=user_nian+"年"+user_yue+"月"+user_day+"日";
		logger.debug("生日：{}",user_date);
		logger.debug("验证 3 获取参数成功：用户名：{},用户真实姓名：{},用户性别：{},用户生日：{}年,{}月,{}日,用户的邮箱：{},用户修改的电话：{},用户账号：{}",username,userzhenshiname,user_sex,user_nian,user_yue,user_day,email,phonenumber,usercode);
		try {
			
			String sql="update user set user_name=?,userzhenshiname=?,user_sex=?,user_date=?,email=?,phonenumber=? where usercode=?";
			ps=conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, userzhenshiname);
			ps.setString(3, user_sex);
			ps.setString(4, user_date);
			ps.setString(5, email);
			ps.setString(6, phonenumber);
			ps.setString(7, usercode);
			
			logger.debug(sql);
			
			if(ps.executeUpdate()!=1) {
				throw new CommException("修改数据失败");
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new CommException(e.getMessage());
		} finally {
			JdbcUtils.close(conn, ps, rs);
		} 
	
	}
	
	
	
	public void shop(String shop_name,double shop_price,String shop_yanse,String shop_gaodu,int shop_shuliang) {
		Connection conn=JdbcUtils.getConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;
		
		
		logger.debug("验证 3    获取参数成功：商品的名称：{},商品的价格：{},商品的颜色：{},商品的高度：{},商品的数量：{}",shop_name,shop_price,shop_yanse,shop_gaodu,shop_shuliang);
		try {
			
			String sql="update shop set shop_name=?,shop_price=?,shop_yanse=?,shop_gaodu=?,shop_shuliang=? where shop_code=1";
			ps=conn.prepareStatement(sql);
			ps.setString(1, shop_name);
			ps.setDouble(2, shop_price);
			ps.setString(3, shop_yanse);
			ps.setString(4, shop_gaodu);
			ps.setInt(5, shop_shuliang);
			
			logger.debug(sql);
			
			if(ps.executeUpdate()!=1) {
				throw new CommException("修改数据失败");
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new CommException(e.getMessage());
		} finally {
			JdbcUtils.close(conn, ps, rs);
		} 

	}
	

}
	
	
	
	
	
	
	
	
	
	
	
	
	
	


